package com.azt.provider.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.azt.api.pojo.ProRepositoryTitles;
import com.azt.api.pojo.ProRepositoryTitlesExcept;
import com.azt.api.service.ProRepositoryTitleService;
import com.azt.model.page.Pagination;
import com.azt.provider.mapper.ProRepositoryTitlesExceptMapper;
import com.azt.provider.mapper.ProRepositoryTitlesMapper;
import com.azt.utils.MyStringUtils;
import com.azt.utils.PasswordUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Service
@Transactional
public class ProRepositoryTitleServiceImpl implements ProRepositoryTitleService {


    @Autowired
    private ProRepositoryTitlesMapper mapper;
    @Autowired
    private ProRepositoryTitlesExceptMapper exceptMapper;


    @Override
    public Pagination getRepositoryPage(int pageno, int pagesize, Map<String, String> searchMap) {


        Example example = new Example(ProRepositoryTitles.class);
        Criteria cr = example.createCriteria();
        String repositoryTitles = searchMap.get("so_repositoryTitles");
        String repositorytype = searchMap.get("so_repositorytype");
        if (StringUtils.isNotBlank(repositorytype) && !"0".equals(repositorytype)) {
            cr.andEqualTo("pid", Integer.parseInt(repositorytype));
        }
        if (StringUtils.isNotBlank(repositoryTitles)) {
            cr.andLike("title", "%" + repositoryTitles + "%");
        }
        example.orderBy("createtime").desc();

        Page<Object> startPage = PageHelper.startPage(pageno, pagesize);
        List<ProRepositoryTitles> selectByExample = mapper.selectByExample(example);

        Pagination p = new Pagination(startPage, selectByExample);
        p.setParams(searchMap);
        return p;
    }


    @Override
    public Integer delrepository(Integer id) {
        ProRepositoryTitles proRepositoryTitles = new ProRepositoryTitles(id);
        return mapper.deleteByPrimaryKey(proRepositoryTitles);
    }


    @Override
    public ProRepositoryTitles getRepositoryTitleById(Integer id) {
        ProRepositoryTitles proRepositoryTitles = new ProRepositoryTitles(id);
        return mapper.selectOne(proRepositoryTitles);
    }

    @Override
    public void save(ProRepositoryTitles repositoryTitle) {
        mapper.insertSelective(repositoryTitle);
    }

    @Override
    public void updateRepositoryTitle(ProRepositoryTitles repositoryTitle) {
        mapper.updateByPrimaryKeySelective(repositoryTitle);
    }

    @Override
    public boolean checkDuplicative(Integer id, String title) {

        ProRepositoryTitles p = new ProRepositoryTitles();
        p.setTitle(title);
        ProRepositoryTitles uniquetitle = mapper.selectOne(p);

        if (uniquetitle != null) {
            if (id != null) {//编辑
                if (uniquetitle.getId() == id) {//自己不用验证
                    return true;
                }
            }
            return false;
        } else {
            return true;
        }
    }

    @Override
    public Pagination getAllExceptPager(int pageno, int pagesize, Map<String, String> searchMap) {
        Page<Object> startPage = PageHelper.startPage(pageno, pagesize);
        List<ProRepositoryTitlesExcept> selectAll = exceptMapper.selectAll();
        Pagination p = new Pagination(startPage, selectAll);
        p.setParams(searchMap);
        return p;
    }

    @Override
    public void delrepositoryExcept(Integer id) {
        exceptMapper.deleteByPrimaryKey(new ProRepositoryTitlesExcept(id));
    }

    @Override
    public String saveExcept(ProRepositoryTitlesExcept except) {
        String title = except.getTitle();
        title = MyStringUtils.replaceBlank(title);
        String md5_16 = PasswordUtil.MD5_16(title);
        ProRepositoryTitlesExcept proRepositoryTitlesExcept = new ProRepositoryTitlesExcept();
        proRepositoryTitlesExcept.setSecret(md5_16);
        ProRepositoryTitlesExcept uniqueByParams = exceptMapper.selectOne(proRepositoryTitlesExcept);
        if (uniqueByParams != null) {
            return "标题重复";
        }

        except.setTitle(title);
        except.setSecret(md5_16);
        exceptMapper.insertSelective(except);
        return null;
    }


    @Override
    public Integer getTitleIdByTitle(String title, int type) {

        String trim = MyStringUtils.replaceBlank(title);
        String md5_16 = PasswordUtil.MD5_16(trim);
        ProRepositoryTitles uniqueByParams = getRepositoryTitleBymd5(md5_16, type);
        if (uniqueByParams == null) {
            return 0;
        }
        return uniqueByParams.getPid();
    }


    @Override
    public ProRepositoryTitles getRepositoryTitleBymd5(String md5_16, int type) {
        Example example = new Example(ProRepositoryTitles.class);
        Criteria criteria = example.createCriteria();
        criteria.andEqualTo("secret", md5_16);
        List<Integer> types = new ArrayList<>();
        types.add(type);
        types.add(0);//全部s
        criteria.andIn("type", types);
        List<ProRepositoryTitles> proRepositoryTitles = mapper.selectByExample(example);
        if (proRepositoryTitles == null || proRepositoryTitles.isEmpty()) {
            return null;
        } else {
            return proRepositoryTitles.get(0);
        }
    }

    @Override
    public ProRepositoryTitles saveOrupdateRepositoryTitle(ProRepositoryTitles title) {
        if (title.getId() == null) {
            mapper.insertSelective(title);
        } else {
            mapper.updateByPrimaryKeySelective(title);
        }
        return title;
    }


    @Override
    public ProRepositoryTitlesExcept getExceptByTitle(String title, Integer type) {
        title = MyStringUtils.replaceBlank(title);
        String md5_16 = PasswordUtil.MD5_16(title);

//		ProRepositoryTitlesExcept proRepositoryTitlesExcept = new ProRepositoryTitlesExcept();
//		proRepositoryTitlesExcept.setSecret(md5_16);
        Example example = new Example(ProRepositoryTitlesExcept.class);
        Criteria criteria = example.createCriteria();
        criteria.andEqualTo("secret", md5_16);
        List<Integer> types = new ArrayList<>();
        types.add(0);
        types.add(type);
        criteria.andIn("type", types);
        List<ProRepositoryTitlesExcept> proRepositoryTitlesExcepts = exceptMapper.selectByExample(example);
        if (proRepositoryTitlesExcepts != null && proRepositoryTitlesExcepts.size() != 0) {
            return proRepositoryTitlesExcepts.get(0);
        } else {
            return null;
        }
    }

}
